package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.SetMealCheckGroupMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkitem")
@Api(tags = "检查项管理接口")
public class CheckitemController {

    @Autowired
    private CheckitemService checkitemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result addItem(@RequestBody CheckItem checkItem){
        log.info("新增检查项:{}", checkItem);
        checkitemService.addItem(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,null);
    }

    /**
     * 根据id删除检查项
     */
    @GetMapping("/delete")
    @ApiOperation("根据id删除检查项")
    public Result deleteItem(@RequestParam Integer id){
        log.info("根据id删除检查项:{}", id);
        checkitemService.deleteItem(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
    }
    /**
     * 根据检查组id查询关联的检查项id
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组id查询关联的检查项id")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer id){
        log.info("根据检查组id查询关联的检查项id:{}", id);
        Integer[] checkItemIds = checkitemService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }
    /**
     * 编辑检查项
     */
    @PostMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项:{}", checkItem);
        checkitemService.editItem(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        PageResult pageResult = checkitemService.findPage(queryPageBean);
        return pageResult;
    }
    /**
     * 查询所有检查项
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItems = checkitemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }
    /**
     * 根据id查询检查项
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查项")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询检查项:{}", id);
        CheckItem checkItem = checkitemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
    /**
     * 根据套餐id查询关联的检查项id
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    @ApiOperation("根据套餐id查询关联的检查项id")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer id){
        log.info("根据套餐id查询关联的检查项id:{}", id);
        List<Integer> checkItemIds = checkitemService.findCheckGroupIdsBySetmealId(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }
}